package com.naihe.anderson.sms.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;

public class MyFileManager extends ListActivity {
	private List<String> items = null;
	private List<String> paths = null;
	private String rootPath = "/mnt/sdcard/";
	private String curPath = "/mnt/sdcard/";
	private TextView mPath;
	
	private String type = "";
	NhSmsApp nhSmsApp;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.fileselect);
		
		 nhSmsApp = (NhSmsApp) getApplication();
	        nhSmsApp.setActivity(this);
	        
		type = getIntent().getStringExtra("type");
		mPath = (TextView) findViewById(R.id.mPath);
		Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
		buttonConfirm.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent data = new Intent(MyFileManager.this, SaveSmsActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("file", curPath);
				data.putExtras(bundle);
				setResult(RESULT_OK, data);
				toFinishActivity();
			}
		});
		Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
		buttonCancle.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				toFinishActivity();
			}
		});
		getFileDir(rootPath);
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 截获按键事件
        if(KeyEvent.KEYCODE_BACK == keyCode){
        	toFinishActivity();
        	return true;
		}
        return super.onKeyDown(keyCode, event);
    }

	private void toFinishActivity(){
		nhSmsApp.setActivity(null);
		MyFileManager.this.finish();
		System.gc();
	}
	
	private void getFileDir(String filePath) {
		if(filePath.equals(rootPath)){
			mPath.setText("选择保存路径");
		} else {
			mPath.setText(filePath);
		}
		items = new ArrayList<String>();
		paths = new ArrayList<String>();
		File f = new File(filePath);
		File[] files = f.listFiles();
		if (!filePath.equals(rootPath)) {
			items.add("b1");
			paths.add(rootPath);
			items.add("b2");
			paths.add(f.getParent());
		}
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			items.add(file.getName());
			paths.add(file.getPath());
		}
		setListAdapter(new MyAdapter(this, items, paths));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		File file = new File(paths.get(position));
		if (file.isDirectory()) {
			curPath = paths.get(position);
			getFileDir(paths.get(position));
		} else {
			if(type.equals("file")){
				curPath = paths.get(position);
				mPath.setText(curPath);
			} else {
				// 可以打开文件
			}
		}
	}

	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		private Bitmap mIcon1;
		private Bitmap mIcon2;
		private Bitmap mIcon3;
		private Bitmap mIcon4;
		private List<String> items;
		private List<String> paths;

		public MyAdapter(Context context, List<String> it, List<String> pa) {
			mInflater = LayoutInflater.from(context);
			items = it;
			paths = pa;
			mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back01);
			mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back02);
			mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder);
			mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc);
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.file_row, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView.findViewById(R.id.text);
				holder.icon = (ImageView) convertView.findViewById(R.id.icon);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			File f = new File(paths.get(position).toString());
			if (items.get(position).toString().equals("b1")) {
				holder.text.setText("返回根目录..");
				holder.icon.setImageBitmap(mIcon1);
			} else if (items.get(position).toString().equals("b2")) {
				holder.text.setText("返回上一层..");
				holder.icon.setImageBitmap(mIcon2);
			} else {
				holder.text.setText(f.getName());
				if (f.isDirectory()) {
					holder.icon.setImageBitmap(mIcon3);
				} else {
					holder.icon.setImageBitmap(mIcon4);
				}
			}
			return convertView;
		}

		private class ViewHolder {
			TextView text;
			ImageView icon;
		}
	}
}